package com.tyao.note.service;
/**
 * 附件、文件业务层
 * @author Ty-user1
 *
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tyao.note.mapper.BAccessoryMapper;
import com.tyao.note.mapper.BFolderMapper;
import com.tyao.note.model.BAccessory;
import com.tyao.note.model.BFolder;
import com.tyao.utils.FileOperate;

@Service
public class AccessoryService{

	@Resource
	private BAccessoryMapper bAccessoryMapper;
	
	@Resource
	private BFolderMapper bFolderMapper;
	
	@Resource
	private FolderService folderService;
	
	/**
	 * 上传文件存入附件表
	 * 
	 * @param accName
	 * @param accPath
	 * @param accExt
	 * @param accSize
	 * @param folderId
	 * @param userId
	 * @return
	 */
	public int saveAcc(String accName, String accPath, String accExt, float accSize, Long folderId, int userId) {
		BAccessory acc = new BAccessory();
		acc.setAccName(accName);
		acc.setAccPath(accPath);
		acc.setAccExt(accExt);
		acc.setUserId(userId);
		acc.setFolderId(folderId);
		acc.setAccSize(accSize);
		return bAccessoryMapper.insert(acc);
	}
	
	public void saveAcc(Long oldFolderId,Long newFolderId) {
		List<BAccessory> accessory= bAccessoryMapper.selectByFolderId(oldFolderId);
		for(BAccessory acc:accessory) {
			acc = bAccessoryMapper.selectByPrimaryKey(acc.getId());
			acc.setFolderId(newFolderId);
			bAccessoryMapper.insert(acc);
		}
	}
	/**
	 * 文件删除，包括删除表记录和磁盘上的文件
	 * @param ids 单个或多个文件id（中间以，隔开）
	 * @return
	 */
	public int accDelete(String ids) {
		List<String> list=new ArrayList<String>();		
		String[] str=ids.split(",");
		//删除磁盘上的文件
		delFiles(str);
		for(int i=0;i<str.length;i++) {
			list.add(str[i]);
		};
		//删除表里的记录
		int flag=bAccessoryMapper.deleteByIds(list);
		return flag;
	}
	/**
	 * 根据附件ids删除磁盘上对应文件
	 * @param ids 处理好的数组类型的文件id，可传多个id
	 */
	public  void delFiles(String[] ids) {
		for(int i=0;i<ids.length;i++) {
			BAccessory acc=bAccessoryMapper.selectByPrimaryKey(Long.parseLong(ids[i]));
			String basePath=folderService.getRootPath();
		    String filePath= folderService.getFolderPathById(acc.getFolderId());
		    String path = basePath+filePath;
			// 文件完整路径
			String allPath = path+'/'+acc.getAccName();
			FileOperate.delFile(allPath);
		}
	}

	/**
	 * 根据附件id查询附件信息
	 * @param id
	 * @return
	 */
	public BAccessory getAccById(Long id) {
		return bAccessoryMapper.selectByPrimaryKey(id);
	}
	/**
	 * 根据搜索条件查询附件
	 * @param folderId
	 * @param fileName
	 * @param beginTime
	 * @param endTime
	 * @return  返回附件列表
	 */
	public List<BAccessory> getAccBySearch(String folderId,String fileName,String beginTime,String endTime,int userId){
		Map map=new HashMap();		
		if(!"".equals(folderId)&&folderId!=null) {
			BFolder bFolder=bFolderMapper.selectDefaultFolder();
			String	defautId=bFolder.getId().toString();
			if(!folderId.equals(defautId)) {
				map.put("folderId", Long.parseLong(folderId));		
			}
		}
		map.put("fileName", fileName);
		if (beginTime != null && beginTime != "") {
			// Date begin= sdf.parse(beginTime+"00:00:00");
			map.put("beginTime", beginTime + " 00:00:00");
		}
		if (endTime != null && endTime != "") {
			map.put("endTime", endTime + " 23:59:59");
		}
		map.put("userId",userId);
		return bAccessoryMapper.fileSearch(map);		
	}
	
	/**
	 * 根据文件夹id查询所有文件
	 * @param folderId
	 * @return
	 */
	public List<BAccessory> getAccByFolder(Long folderId){
		return bAccessoryMapper.selectByFolderId(folderId);
	}
	
	/**
	 * 移动单个文件时修改表中记录
	 * @param fileId  文件id
	 * @param newFolderId  移动到的新文件夹id
	 * @return
	 */
	public int updateFolderId(Long fileId,Long newFolderId) {
		//BAccessory accessory=bAccessoryMapper.selectByPrimaryKey(fileId);
		BAccessory record=new BAccessory();
		record.setFolderId(newFolderId);
		record.setId(fileId);
		return bAccessoryMapper.updateByNewFolder(record);		
	}
}
